package com.ocom.accesservice.controller;

import com.github.pagehelper.PageInfo;
import com.ocom.accesservice.service.DeviceLoginService;
import com.ocom.accesservice.service.MjDeviceInfoService;
import com.ocom.accesservice.vo.devicelogin.GetLoginAccountVo;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.master.EntityDeviceLogin;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class LoginAccountController {
    private final Logger log = LoggerFactory.getLogger(LoginAccountController.class);

    @Autowired
    private DeviceLoginService deviceLoginService;

    @Autowired
    private MjDeviceInfoService deviceInfoService;
    /**
     * 获取登录账号（getLoginAccount）
     * */
    @PostMapping(AccessServiceUrl.URL_GetLoginAccount)
    public Result GetLoginAccount(@Validated @RequestBody GetLoginAccountVo getLoginAccountVo){
        if (getLoginAccountVo.getPage()==null){
            log.error("获取登录账号页码page为空");
            return ResultUtil.error(ResultEnum.Err_203,"获取登录账号页码page为空");
        }
        if (getLoginAccountVo.getSize()==null){
            log.error("获取登录账号页数size为空");
            return ResultUtil.error(ResultEnum.Err_203,"获取登录账号页数size为空");
        }
        Map<String, Object> map = new HashMap<>();
        try {
            List<EntityDeviceLogin> entityDeviceLogins = deviceLoginService.GetLoginAccount(getLoginAccountVo);
            PageInfo<EntityDeviceLogin> entityDeviceLoginPageInfo = new PageInfo<>(entityDeviceLogins);
            map.put("total",entityDeviceLoginPageInfo.getTotal());
            map.put("list",entityDeviceLogins);
        }catch (Exception exception){
            log.error("获取登录账号列表失败："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"获取登录账号列表失败");
        }
       return ResultUtil.success(map);
    }

    /**
     * 修改登录账号（updateLoginAccount）【WEB】
     * */
    @PostMapping(AccessServiceUrl.URL_UpdateLoginAccount)
    public Result UpdateLoginAccount(@Validated @RequestBody EntityDeviceLogin deviceLogin){
        if (deviceLogin.getComId()==null){
            log.error("修改登录账号企业编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"修改登录账号企业编号为空");
        }
        if (deviceLogin.getDeviceNo()==null){
            log.error("修改登录账号设备编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"修改登录账号设备编号为空");
        }
        if (deviceLogin.getId()==null){
            log.error("修改登录账号id为空");
            return ResultUtil.error(ResultEnum.Err_203,"修改登录账号id为空");
        }
        try {
            deviceLoginService.UpdateLoginAccount(deviceLogin);
        }catch (Exception exception){
            log.error("修改登录账号失败："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"修改登录账号失败");
        }
        try {
            String jobId = DateUtils.dateTimeNow();
            Long state = 1L;
            deviceInfoService.SetMachParaState(deviceLogin.getComId(),deviceLogin.getDeviceNo(),jobId,state);
        }catch (Exception exception){
            log.error("修改设备标识失败："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"修改设备标识失败");
        }
        return ResultUtil.success();
    }


    /**
     * 新增登录账号（addLoginAccount）【WEB】
     * */
    @PostMapping(AccessServiceUrl.URL_AddLoginAccount)
    public Result AddLoginAccount(@Validated @RequestBody EntityDeviceLogin deviceLogin){
        if (deviceLogin.getComId()==null){
            log.error("新增登录账号信息企业编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"新增登录账号信息企业编号为空");
        }
        if (deviceLogin.getDeviceNo()==null){
            log.error("新增登录账号信息设备编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"新增登录账号信息设备编号为空");
        }
        if (deviceLogin.getLoginType()==null){
            log.error("新增登录账号信息登录类型为空");
            return ResultUtil.error(ResultEnum.Err_203,"新增登录账号信息登录类型为空");
        }
        if (deviceLogin.getAccount()==null){
            log.error("新增登录账号信息登录账号为空");
            return ResultUtil.error(ResultEnum.Err_203,"新增登录账号信息登录账号为空");
        }
        if (deviceLogin.getPassword()==null){
            log.error("新增登录账号信息登录密码为空");
            return ResultUtil.error(ResultEnum.Err_203,"新增登录账号信息登录密码为空");
        }
        try {
            deviceLoginService.AddLoginAccount(deviceLogin);
        }catch (Exception exception){
            log.error("新增登录账号信息失败："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"新增登录账号信息失败");
        }
        try {
            String jobId = DateUtils.dateTimeNow();
            Long state = 1L;
            deviceInfoService.SetMachParaState(deviceLogin.getComId(),deviceLogin.getDeviceNo(),jobId,state);
        }catch (Exception exception){
            log.error("修改设备标识失败："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"修改设备标识失败");
        }
        return ResultUtil.success();
    }

    /**
     * 删除登录账号（deleteLoginAccount）【WEB】
     * */
    @PostMapping(AccessServiceUrl.URL_DeleteLoginAccount)
    public Result DeleteLoginAccount(Long comId,String ids){
        if (comId==null){
            log.error("删除登录账号信息企业编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"删除登录账号信息企业编号为空");
        }
        if (ids.isEmpty()){
            log.error("删除登录账号信息id为空");
            return ResultUtil.error(ResultEnum.Err_203,"删除登录账号信息id为空");
        }
        try {
            String[] idStr = ids.split(",");
            for (String id: idStr) {
                EntityDeviceLogin entityDeviceLogin = deviceLoginService.DeleteLoginAccount(comId, Long.valueOf(id));
                String jobId = DateUtils.dateTimeNow();
                Long state = 1L;
                deviceInfoService.SetMachParaState(comId,entityDeviceLogin.getDeviceNo(),jobId,state);
            }

        }catch (Exception exception){
            log.error("删除登录账号信息失败："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"删除登录账号信息失败");
        }
        return ResultUtil.success();
    }
}
